export default class gameScene3 extends Laya.Script {

    constructor() {
        super();
        gameScene3._intance = this
    }
    private trap1: Laya.Sprite              //地刺1
    private trap2: Laya.Sprite              //地刺2

    private rank_btn: Laya.Button          //排行榜按钮
    private select_btn: Laya.Button        //进入选择关卡
    private sound_btn: Laya.Button         //音乐按钮
    private un_sound_btn: Laya.Button       //关闭音乐
    private again_btn: Laya.Button         //重新开始按钮
    private UI_panel: Laya.Sprite          //萝卜计数面板
    private turnip_txt: Laya.Text          //萝卜数文本
    public turnip_num: number = 0           //萝卜计数
    private house: Laya.Sprite             //家
    private door: Laya.Sprite              //门
    private enter: Laya.Sprite             //进入门碰撞
    private timer_panel: Laya.Sprite       //计时面板
    private timer_txt: Laya.Text           //计时文本
    public timer_num: number = 0          //计时初始值

    public num: number = 1                 //每秒计时
    public static _intance: gameScene3      //脚本赋给

    onEnable(): void {
        //初始化变量
        this.initParams()
        //UI动画
        this.enterAni_1()
        //机关动画
        this.enterAni_2()
        //初始化绑定事件
        this.initEvent()
        //计时
        this.count()
    }

    initEvent() {
        //给排行榜按钮绑定事件
        this.rank_btn.on(Laya.Event.CLICK, this, this.onRank)
        //给选择关按钮绑定事件
        this.select_btn.on(Laya.Event.CLICK, this, this.onSelect)
        //给音乐按钮绑定事件
        this.sound_btn.on(Laya.Event.CLICK, this, this.onSound)
        //给关闭音乐按钮绑定事件
        this.un_sound_btn.on(Laya.Event.CLICK, this, this.onSound)
        //给重新开始按钮绑定事件
        this.again_btn.on(Laya.Event.CLICK, this, this.onAgain)
    }

    /**打开场景 */
    onRank() {
        Laya.Scene.open("rank_scene.scene")
    }
    onSelect() {
        Laya.Scene.open("select_scene.scene")
        //播放bgm1
        // Laya.SoundManager.playMusic("music/bg1.mp3", 0)
    }
    onSound() {
        if (this.sound_btn.visible) {
            this.sound_btn.visible = false;
            Laya.SoundManager.stopAll()
        }
        else {
            this.sound_btn.visible = true;
        }
        if (this.un_sound_btn.visible) {
            this.un_sound_btn.visible = false;
            Laya.SoundManager.playMusic("music/bg2.mp3", 0)
        }
        else {
            this.un_sound_btn.visible = true;
        }
    }
    onAgain() {
        //重置面板，重新开始游戏
        Laya.Scene.open("gameScene3.scene")
    }

    enterAni_1() {
        Laya.Tween.from(
            this.rank_btn,
            { y: -30 },
            800,
            Laya.Ease.strongInOut
        )
        Laya.Tween.from(
            this.select_btn,
            { y: -500 },
            800,
            Laya.Ease.strongInOut
        )
        Laya.Tween.from(
            this.again_btn,
            { y: -1000 },
            800,
            Laya.Ease.strongInOut
        )
        Laya.Tween.from(
            this.sound_btn,
            { y: -1500 },
            800,
            Laya.Ease.strongInOut
        )
        Laya.Tween.from(
            this.UI_panel,
            { y: -1000 },
            800,
            Laya.Ease.strongInOut
        )
    }

    enterAni_2() {
        //地刺1动画
        Laya.Tween.to(
            this.trap1,
            { y: 650 },
            1500,
            Laya.Ease.strongInOut,
            Laya.Handler.create(this, () => {
                Laya.Tween.to(
                    this.trap1,
                    { y: 495 },
                    2500,
                    Laya.Ease.strongInOut
                )
            })
        )
        Laya.timer.loop(4000, this, () => {
            Laya.Tween.to(
                this.trap1,
                { y: 650 },
                1500,
                Laya.Ease.strongInOut,
                Laya.Handler.create(this, () => {
                    Laya.Tween.to(
                        this.trap1,
                        { y: 495 },
                        2500,
                        Laya.Ease.strongInOut
                    )
                })
            )
        })
        //地刺2动画
        Laya.Tween.to(
            this.trap2,
            { y: 650 },
            1500,
            Laya.Ease.strongInOut,
            Laya.Handler.create(this, () => {
                Laya.Tween.to(
                    this.trap2,
                    { y: 495 },
                    2500,
                    Laya.Ease.strongInOut
                )
            })
        )
        Laya.timer.loop(4000, this, () => {
            Laya.Tween.to(
                this.trap2,
                { y: 650 },
                1500,
                Laya.Ease.strongInOut,
                Laya.Handler.create(this, () => {
                    Laya.Tween.to(
                        this.trap2,
                        { y: 495 },
                        2500,
                        Laya.Ease.strongInOut
                    )
                })
            )
        })
    }

    initParams() {
        //地刺节点
        this.trap1 = this.owner.getChildByName("trap1") as Laya.Sprite
        this.trap2 = this.owner.getChildByName("trap2") as Laya.Sprite

        //UI和按钮节点
        //获取排行榜按钮节点
        this.rank_btn = this.owner.getChildByName('rank_btn') as Laya.Button
        //获取选择关按钮节点
        this.select_btn = this.owner.getChildByName('select_btn') as Laya.Button
        //获取再来一次
        this.again_btn = this.owner.getChildByName('again_btn') as Laya.Button
        //获取声音节点
        this.sound_btn = this.owner.getChildByName('voice_btn') as Laya.Button
        //获取关闭声音按钮节点
        this.un_sound_btn = this.owner.getChildByName('un_voice_btn') as Laya.Button
        //获取UI面板节点
        this.UI_panel = this.owner.getChildByName('UI_panel') as Laya.Button
        //获取萝卜数文本节点
        this.turnip_txt = this.UI_panel.getChildByName('turnip_txt') as Laya.Text
        //获取家节点
        this.house = this.owner.getChildByName("house") as Laya.Sprite
        //获取门节点
        this.door = this.house.getChildByName("door") as Laya.Sprite
        //获取进入门碰撞节点
        this.enter = this.owner.getChildByName("enter") as Laya.Sprite
        //计时面板结点
        this.timer_panel = this.owner.getChildByName("timer_panel") as Laya.Sprite
        //计时文本节点
        this.timer_txt = this.timer_panel.getChildByName("timer_txt") as Laya.Text
    }

    //增加分数调用
    addSum(data: any) {
        this.turnip_num += data;
        this.turnip_txt.text = ":  " + this.turnip_num + "/7"

        //游戏结束判断
        if (this.turnip_num == 7) {
            Laya.Tween.to(
                this.enter,
                { y: 512 },
                100,
                Laya.Ease.strongInOut
            )
            Laya.Tween.to(
                this.door,
                { x: 12 },
                1000,
                Laya.Ease.strongInOut
            )
        }
    }

    /**计时 */
    count() {
        //定时执行countDown函数
        Laya.stage.timerLoop(
            1000,
            this,
            this.countDown
        )
    }
    countDown() {
        this.timer_num += this.num
        this.timer_txt.text = "时间: " + this.timer_num + "s"
    }

    onDisable(): void {
    }
}